<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/6/26
 * Time: 1:53 PM
 */
namespace app\common\models;

class CoreAttach extends BaseModel
{
    public $table = 'yq_core_attach';
    public $timestamps = true;
    protected $guarded = [''];

    // 存储在表中type字段的对应的类型
    const IMAGE_TYPE = 1;// 图片 1
    const AUDIO_TYPE = 2;// 音频 2
    const VIDEO_TYPE = 3;// 视频 3
    const FILE_TYPE = 4;// 文件 4

    // 存储在表中upload_type字段的对应的类型
    const UPLOAD_LOCAL = 0;     // 本地
    const UPLOAD_ALIOSS = 1;       // 阿里云
    const UPLOAD_QCLOUDCOS = 2;       // 腾讯云

    public function scopeSearch($query, $search)
    {
        if ($search['month'] && $search['year']) {

            return $query->whereBetween('created_at', [

                mktime(0,0,0, $search['month'], 1, $search['year']),
                mktime(23,59,59, $search['month']+1, 0, $search['year'])
            ]);
        }

        if ($search['year']) {
            return $query->whereBetween(
                'created_at',
                [
                    mktime(0,0,0, 1, 1, $search['year']),
                    mktime(23,59,59,12, 31, $search['year'])
                ]
            );
        }

        if ($search['month']) {
            return $query->whereBetween(
                'created_at',
                [
                    mktime(0,0,0, $search['month'], 1, date('Y')),
                    mktime(23,59,59, $search['month']+1, 0, date('Y'))
                ]
            );
        }
        if ($search['type']) {
            return $query->where('type', $search['type']);
        }
    }

    public function atributeNames()
    {
        return [
            'uid' => '用户id',
            'filename' => '原文件名',
            'attachment' => '新文件名',
        ];
    }
    public function rules()
    {
        return [
            'uid' => 'integer',
            'filename' => 'string|max:50',
            'attachment' => '',
        ];
    }
}